#!/bin/bash

DIR=`pwd`
Setupwin10=SetupTool_win10_64bit-V$1
Setupwin7=SetupTool_win7_64bit-V$1

Bootmgrwin10=Bootmgr_win10_64bit-V$1
Bootmgrwin7=Bootmgr_win7_64bit-V$1


Setupwin10DIR=$DIR/$Setupwin10
Setupwin7DIR=$DIR/$Setupwin7

Bootmgrwin10DIR=$DIR/$Bootmgrwin10
Bootmgrwin7DIR=$DIR/$Bootmgrwin7

BootPRO=klbootmgr-V$1
PRODIR=$DIR/$PRO
BootPRODIR=$DIR/$BootPRO

TOOLDIR="$(dirname "$0")"

FLASHROMDIR=$TOOLDIR/../FlashRom

FLASHROMWIN10=(../$FLASHROMDIR/win_64/flashrom.exe)
FLASHROMWIN764=(../$FLASHROMDIR/win7_64/flashrom.exe)

UEFIDIR=$TOOLDIR/../uefitool-new_engine/UEFIExtract
IFRDIR=$TOOLDIR/../universal-ifr-extractor-master
CONMODDIR=$TOOLDIR/../universal-ifr-extractor-master/config_modify
BIOSVENDOR="KunlunTech"
COPYRIGHT="Kunlun BIOS, Kunlun Technology (Beijing) Co., Ltd."
PACKDIR=$TOOLDIR/pack

if (( $# <= 0 )) ; then
  echo "Please add version, for example : 1.4.1 [clean]"
  echo " $0  "
  echo "Copyright (c) 2005-2022, Kunlun BIOS, Kunlun Technology (Beijing) Co., Ltd."
  exit 1;
fi

if [[ $* =~ "clean" ]] ; then
  echo clean $Setupwin10
  echo rm $Setupwin10 $Setupwin10.zip
  rm -rf $Setupwin10 $Setupwin10.zip

  echo clean $Setupwin7
  echo rm $Setupwin7 $Setupwin7.zip
  rm -rf $Setupwin7 $Setupwin7.zip

  echo clean $Bootmgrwin10
  echo rm $Bootmgrwin10 $Bootmgrwin10.zip
  rm -rf $Bootmgrwin10 $Bootmgrwin10.zip

  echo clean $Bootmgrwin7
  echo rm $Bootmgrwin7 $Bootmgrwin7.zip
  rm -rf $Bootmgrwin7 $Bootmgrwin7.zip

  exit 0
elif [ -n "$2" ]; then
  BIOSVENDOR="$2"
fi

if [ -n "$3" ]; then
  COPYRIGHT="$3"
fi

mkdir $Setupwin10

mkdir $Setupwin7

cd $FLASHROMDIR 
make clean 
rm ./win_32/flashrom.exe
rm ./win_64/flashrom.exe
rm ./win7_64/flashrom.exe
chmod 777 ./util/getrevision.sh
make CONFIG_STATIC=yes CONFIG_FT2232_SPI=no CONFIG_USBBLASTER_SPI=no  CONFIG_ENABLE_LIBUSB1_PROGRAMMERS=no  $2
cd $DIR

cd $UEFIDIR
rm -rf CMakeCache.txt  CMakeFiles
cmake -G "Unix Makefiles" .
make $2
cd $DIR

cd $IFRDIR
rm -rf CMakeCache.txt  CMakeFiles
cmake -G "Unix Makefiles" .
make $2
cd $DIR

cd $CONMODDIR
rm config_modify_tool*
gcc -static main.c cJSON.c -lm -o config_modify_tool
echo "gcc -static main.c cJSON.c -lm -o config_modify_tool"
cd $DIR

cd $PACKDIR
make clean
make BIOS_VENDOR="$BIOSVENDOR" COPY_RIGHT="$COPYRIGHT"
cd $DIR


cd $Setupwin10DIR
FLASHROMWIN10=(../$FLASHROMDIR/win_64/flashrom.exe )
TOOLFILE=(../$IFRDIR/klsetuptool.sh ../$IFRDIR/setuptool.sh ../$IFRDIR/setuptool_layout.sh ../$IFRDIR/ifrextract.exe  ../$IFRDIR/config_modify/config_modify_tool.exe   ../$UEFIDIR/UEFIExtract.exe)
../$PACKDIR/Pack SetupTool.exe 8 ../$PACKDIR/Setup.exe ${TOOLFILE[*]} ${FLASHROMWIN10[*]}

cp -r ../$TOOLDIR/SetupTool_ReleaseNote.txt ./
cp -r ../$TOOLDIR/SetupTool_readme.txt ./

cp -r ../$PACKDIR/init.bat ./
cp -r ../$PACKDIR/vc_redist.x64.exe ./
cp -r ../$TOOLDIR/KlWin_x64/KlWinEnvSetup ./
cp -r ../$TOOLDIR/windows/SetupUi_64/* ./

cd $DIR

zip -qr $Setupwin10.zip  $Setupwin10/*


cd  $Setupwin7DIR


FLASHROMWIN7=(../$FLASHROMDIR/win7_64/flashrom.exe )
TOOLFILE=(../$IFRDIR/klsetuptool.sh ../$IFRDIR/setuptool.sh ../$IFRDIR/setuptool_layout.sh ../$IFRDIR/ifrextract.exe  ../$IFRDIR/config_modify/config_modify_tool.exe   ../$UEFIDIR/UEFIExtract.exe)
../$PACKDIR/Pack SetupTool.exe 8 ../$PACKDIR/Setup.exe ${TOOLFILE[*]} ${FLASHROMWIN7[*]}  

cp -r ../$TOOLDIR/SetupTool_ReleaseNote.txt ./
cp -r ../$TOOLDIR/SetupTool_readme.txt ./

cp -r ../$PACKDIR/init.bat ./
#cp -r ../$PACKDIR/vc_redist.x64.exe ./
#cp -r ../$TOOLDIR/KlWin_x64/KlWinEnvSetup ./ 
#cp -r ../$TOOLDIR/windows/SetupUi_64/* ./

cd $DIR

#zip -qr $Setupwin7.zip  $Setupwin7/*

mkdir $Bootmgrwin10

mkdir $Bootmgrwin7

cd  $Bootmgrwin10DIR

BOOTFILE=(../$IFRDIR/kltool_bootmgr.sh ../$IFRDIR/kltool_bootcfg.sh  $Setupwin10DIR/SetupTool.exe )
../$PACKDIR/Pack klbootmgr.exe 4 ../$PACKDIR/Setup.exe    ${BOOTFILE[*]}  

cp -r ../$TOOLDIR/klbootmgr_ReleaseNote.txt ./
cp -r ../$TOOLDIR/klbootmgr_readme.txt ./


cp -r ../$PACKDIR/init.bat ./
cp -r ../$PACKDIR/vc_redist.x64.exe ./
cp -r ../$TOOLDIR/KlWin_x64/KlWinEnvSetup ./

cd $DIR

zip -qr $Bootmgrwin10.zip  $Bootmgrwin10/*



cd  $Bootmgrwin7DIR

BOOTFILE=(../$IFRDIR/kltool_bootmgr.sh ../$IFRDIR/kltool_bootcfg.sh  $Setupwin7DIR/SetupTool.exe )
../$PACKDIR/Pack klbootmgr.exe 4 ../$PACKDIR/Setup.exe    ${BOOTFILE[*]}  

cp -r ../$TOOLDIR/klbootmgr_ReleaseNote.txt ./
cp -r ../$TOOLDIR/klbootmgr_readme.txt ./


cp -r ../$PACKDIR/init.bat ./
#cp -r ../$PACKDIR/vc_redist.x64.exe ./
#cp -r ../$TOOLDIR/KlWin_x64/KlWinEnvSetup ./

cd $DIR

#zip -qr $Bootmgrwin7.zip  $Bootmgrwin7/*

echo  "build all OK"
